import java.io.*;
import java.util.*;

public class Main {

    // 球队类：存储和更新球队数据
    static class Team {
        String name;           // 球队名
        int matchesPlayed;     // 场次
        int wins;              // 胜场
        int draws;             // 平局
        int losses;            // 负场
        int goalsScored;       // 进球
        int goalsConceded;     // 失球
        int points;            // 积分

        // 构造方法
        public Team(String name) {
            this.name = name;
            this.matchesPlayed = 0;
            this.wins = 0;
            this.draws = 0;
            this.losses = 0;
            this.goalsScored = 0;
            this.goalsConceded = 0;
            this.points = 0;
        }

        // 更新比赛结果
        public void updateMatch(int goalsFor, int goalsAgainst) {
            this.matchesPlayed++;
            this.goalsScored += goalsFor;
            this.goalsConceded += goalsAgainst;

            if (goalsFor > goalsAgainst) {
                // 赢
                this.wins++;
                this.points += 3;
            } else if (goalsFor == goalsAgainst) {
                // 平局
                this.draws++;
                this.points += 1;
            } else {
                // 输
                this.losses++;
            }
        }

        // 计算净胜球
        public int getGoalDifference() {
            return this.goalsScored - this.goalsConceded;
        }

        @Override
        public String toString() {
            return String.format("%-15s %4d %4d %4d %4d %4d %4d %6d %4d",
                    name, matchesPlayed, wins, draws, losses, goalsScored, goalsConceded, getGoalDifference(), points);
        }
    }

    // 联赛管理类
    static class League {
        private List<Team> teams;

        public League() {
            this.teams = new ArrayList<>();
        }

        // 查找球队，如果不存在则添加
        public Team getTeam(String name) {
            for (Team team : teams) {
                if (team.name.equals(name)) {
                    return team;
                }
            }
            Team newTeam = new Team(name);
            teams.add(newTeam);
            return newTeam;
        }

        // 处理一场比赛
        public void processMatch(String homeTeamName, String awayTeamName, int homeScore, int awayScore) {
            Team homeTeam = getTeam(homeTeamName);
            Team awayTeam = getTeam(awayTeamName);

            homeTeam.updateMatch(homeScore, awayScore);
            awayTeam.updateMatch(awayScore, homeScore);
        }

        // 排序球队
        public void sortTeams() {
            teams.sort((t1, t2) -> {
                if (t2.points != t1.points) {
                    return t2.points - t1.points; // 按积分排序（降序）
                } else if (t2.getGoalDifference() != t1.getGoalDifference()) {
                    return t2.getGoalDifference() - t1.getGoalDifference(); // 按净胜球排序（降序）
                } else {
                    return t2.goalsScored - t1.goalsScored; // 按进球数排序（降序）
                }
            });
        }

        // 显示积分表
        public void displayTable() {
            System.out.println(String.format("%-15s %4s %4s %4s %4s %4s %4s %6s %4s",
                    "球队", "场次", "胜场", "平局", "负场", "进球", "失球", "净胜球", "积分"));
            for (Team team : teams) {
                System.out.println(team);
            }
        }

        // 保存积分表到文件
        public void saveToFile(String filename) throws IOException {
            try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) {
                writer.write(String.format("%-15s %4s %4s %4s %4s %4s %4s %6s %4s\n",
                        "球队", "场次", "胜场", "平局", "负场", "进球", "失球", "净胜球", "积分"));
                for (Team team : teams) {
                    writer.write(team.toString());
                    writer.newLine();
                }
            }
        }

        // 从文件加载积分表
        public void loadFromFile(String filename) throws IOException {
            try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
                String line = reader.readLine(); // 跳过表头
                while ((line = reader.readLine()) != null) {
                    String[] parts = line.split("\\s+");
                    String name = parts[0];
                    Team team = getTeam(name);
                    team.matchesPlayed = Integer.parseInt(parts[1]);
                    team.wins = Integer.parseInt(parts[2]);
                    team.draws = Integer.parseInt(parts[3]);
                    team.losses = Integer.parseInt(parts[4]);
                    team.goalsScored = Integer.parseInt(parts[5]);
                    team.goalsConceded = Integer.parseInt(parts[6]);
                    team.points = Integer.parseInt(parts[8]);
                }
            }
        }
    }

    // 主程序
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        League league = new League();

        // 从文件加载积分表
        String filename = "league_table.txt";
        try {
            league.loadFromFile(filename);
        } catch (FileNotFoundException e) {
            System.out.println("积分表文件未找到，将创建新的积分表。");
        }

        // 输入比赛结果
        System.out.println("请输入比赛结果 (格式: 主队 客队 主队得分 客队得分), 输入 'exit' 结束:");
        while (true) {
            String input = scanner.nextLine();
            if (input.equalsIgnoreCase("exit")) {
                break;
            }

            String[] parts = input.split("\\s+");
            if (parts.length != 4) {
                System.out.println("输入格式错误，请重新输入！");
                continue;
            }

            String homeTeam = parts[0];
            String awayTeam = parts[1];
            int homeScore, awayScore;

            try {
                homeScore = Integer.parseInt(parts[2]);
                awayScore = Integer.parseInt(parts[3]);
            } catch (NumberFormatException e) {
                System.out.println("比分必须是整数，请重新输入！");
                continue;
            }

            league.processMatch(homeTeam, awayTeam, homeScore, awayScore);
        }

        // 排序并显示积分表
        league.sortTeams();
        league.displayTable();

        // 保存积分表到文件
        league.saveToFile(filename);
        System.out.println("最新积分表已保存到文件: " + filename);
    }
}